﻿using NoteSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NoteSystem
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            #region 免登录
                //有cookie获取账号和密码
                //1.判断是否有cookie
                if (Request.Cookies["account"] != null & Request.Cookies["pwd"] != null)
                {
                    string account = Request.Cookies["account"].Value;
                    string pwd = Request.Cookies["pwd"].Value;
                //2.如果有cookie则获取账号密码进行验证
                //3.存session
                //4.跳页
                DataSet ds = DbHelper.Query($@"select u.*,c.CityName from users u inner join S_City c on CityID=city_id where account='{account}' and pwd='{pwd}'");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        int type = Convert.ToInt32(ds.Tables[0].Rows[0]["type"].ToString());
                        if (type==0)
                        {
                            Response.Write("<script>alert('审核未通过，请稍后再试！')</script>");
                        }
                        else
                        {
                            Session["User"]=ds.Tables[0].Rows[0];
                            Response.Redirect("Index.aspx");
                        }
                       
                    }
                }
            #endregion
        }

        #region 登录 
        protected void Button1_Click(object sender, EventArgs e)
        {
            string account = txtAccount.Text.Trim();
            string pwd = txtPwd.Text.Trim();
            string code = Session["Code"].ToString();
            string userCode =txtCode.Text.Trim();
            if(userCode!=code)
            {
                Response.Write("<script>alert('验证码错误')</script>");
                return;
            }
            DataSet ds = DbHelper.Query($@"select u.*,c.CityName from users u inner join S_City c on CityID=city_id where account='{account}' and pwd='{pwd}'");
            if (ds.Tables[0].Rows.Count==0)
            {
                Response.Write("<script>alert('账号密码错误！')</script>");
                return;
            }
            int type = Convert.ToInt32(ds.Tables[0].Rows[0]["type"]);
            if(type==0)
            {
                Response.Write("<script>alert('请等待管理员审核！')</script>");
                return;
            }
            //存session
            Session["User"]=ds.Tables[0].Rows[0];//把当前登陆人的整行字段信息全部存入session 
            if (cbxRMe.Checked)
            {
                //记住我 存cookie
                //账号密码存到Cookie里
                HttpCookie accountCookie = new HttpCookie("account");
                accountCookie.Value=ds.Tables[0].Rows[0]["account"].ToString();
                accountCookie.Expires=DateTime.Now.AddMonths(1);
                Response.Cookies.Add(accountCookie);
                HttpCookie pwdCookie = new HttpCookie("pwd");
                pwdCookie.Value=ds.Tables[0].Rows[0]["pwd"].ToString();
                pwdCookie.Expires=DateTime.Now.AddMonths(1);
                Response.Cookies.Add(pwdCookie);
            }
            Response.Redirect("Index.aspx");

            //if (ds.Tables[0].Rows.Count>0)
            //{
            //    if (cbxRMe.Checked)
            //    {
            //        //记住我 存cookie
            //        //账号密码存到Cookie里
            //        HttpCookie accountCookie = new HttpCookie("account");
            //        accountCookie.Value=ds.Tables[0].Rows[0]["account"].ToString();
            //        accountCookie.Expires=DateTime.Now.AddMonths(1);
            //        Response.Cookies.Add(accountCookie);
            //        HttpCookie pwdCookie = new HttpCookie("pwd");
            //        pwdCookie.Value=ds.Tables[0].Rows[0]["pwd"].ToString();
            //        pwdCookie.Expires=DateTime.Now.AddMonths(1);
            //        Response.Cookies.Add(pwdCookie);
            //    }
            //    //存session
            //    Session["User"]=ds.Tables[0].Rows[0];//把当前登陆人的整行字段信息全部存入session 
            //    int type = Convert.ToInt32(ds.Tables[0].Rows[0]["type"].ToString());
            //    if (type==0)
            //    {
            //       Response.Write("<script>alert('请等待管理员审核！')</script>");
            //    }
            //    else
            //    {
            //        //跳页
            //        Response.Redirect("Index.aspx");
            //    }
               
            //}
            //else
            //{
            //    Response.Write("<script>alert('账号或密码不正确')</script>");
            //}
        }
    }
    #endregion
}